/**
 * Copyright 2018 by FREEMYPAL.
 * Tianhua Information Technology Park (South) Building 4, 4th Floor, Longtian Road 195,Shanghai, P.R. China，200235. 
 * All rights reserved.
 * This software is the confidential and proprietary information of FREEMYPAL
 * ("Confidential Information"). You shall not disclose such
 * Confidential Information and shall use it only in accordance with the terms
 * of the license agreement you entered into with FREEMYPAL.
 */
package com.roc.web.action.auditStatistics;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.roc.bean.AuditStatisticsBean;
import com.roc.newPage.IPageService;
import com.roc.page.BaseSearchCondition;
import com.roc.page.PaginationBaseAction;
import com.roc.pojo.Operators;
import com.roc.service.ServiceException;
import com.roc.util.Constance;
import com.roc.util.constant.Constant;
import com.roc.util.constant.Role;

/**
 * @Description: 云商宝实名审核统计
 * @author JunFei.Zhao
 * @version 1.0 2018年3月6日
 */
@Action
@Component
@Scope("prototype")
@Namespace("/auditStatistics")
public class AuditStatisticsAction extends PaginationBaseAction {

	private static final long serialVersionUID = 1L;

	private Logger logger = LogManager.getLogger(AuditStatisticsAction.class);

	private AuditStatisticsSearchCondition searchCondition;

	@Autowired
	private IPageService pageService;

	 @Autowired
	IPageService pagenitionService;


	/**
	 * 审核统计  跳转主页面
	 * @Title: init
	 * @Description: TODO
	 * @param @return
	 * @return String
	 * @throws
	 */
	@Action(value = "init", results = { @Result(name = "success", location = "/pages/auditStatistics/AuditStatisticsJsp.jsp") })
	public String init() {
		Operators op = (Operators) getSession().getAttribute(Constant.SESSION_LOGIN_MERCHANT);
		if (op.getRoleType().equals(Role.RISK_CONTROL_LEAD.getCode())
				|| op.getRoleType().equals(Role.SYSTEM.getCode())) {
			return SUCCESS;
		}
		addActionError("无权限访问!!!");
		return ERROR;
	}

	/**
	 * 审核统计查询
	 * @Title: queryAuditStatistics
	 * @Description: TODO
	 * @param @return
	 * @return String
	 * @throws
	 */
	@Action(value = "queryAuditStatistics", results = { @Result(name = "success", location = "/pages/auditStatistics/AuditStatisticsJsp.jsp") })
	public String queryAuditStatistics() {

		if (searchCondition == null) {
			searchCondition = (AuditStatisticsSearchCondition) getSearchConditionFromSession();
			if (searchCondition == null) {
				searchCondition = new AuditStatisticsSearchCondition();
			}
		}

		logger.info("查询条件时间为：" + searchCondition.getAuditDate());

		try {

			int rows=pagenitionService.countSQL(searchCondition);
			if(rows>0){
				searchRsList=pagenitionService.findAllSQL(searchCondition, calcuStart(getCurrentPage()), Constance.PAGE_MAX_SIZE);
			}
			
			List<AuditStatisticsBean> asbList = new ArrayList<AuditStatisticsBean>();
			dealWith(asbList,searchRsList);
			
			getRequest().setAttribute("searchRsList", asbList);
			this.setTotalPage(this.calcuTotalPage(rows));
			getRequest().setAttribute(CURRENT_PAGE, this.getCurrentPage());
			getRequest().setAttribute(TOTAOL_PAGE, this.getTotalPage());
			setSearchToConditionSession(searchCondition);
			addActionMessage("当前条件下统计到【" + rows + "】条记录");
		} catch (Exception e) {
			logger.info("查询审核统计数据异常：" + e);
		}
		return SUCCESS;
	}
	
	
	
	private List<AuditStatisticsBean> dealWith(List<AuditStatisticsBean> asbList,List<Object> searchRsList) throws ServiceException {
		if(searchRsList!=null&&searchRsList.size()>0)
		{
			int sumPass = 0;
			int sumNoPass = 0;
			int sumLimit = 0;
			int sumUnLimit = 0;
			int sumTotal = 0;
			Iterator<?> iterator=searchRsList.iterator();
			while(iterator.hasNext())
			{
				Object[] obj=(Object[]) iterator.next();
				AuditStatisticsBean asb=new AuditStatisticsBean();
				asb.setAuditor(Constance.Null2EmptyTrim(obj[0]));
				
					sumTotal=Integer.parseInt(Constance.Null2EmptyTrim(obj[1]))+sumTotal;
				asb.setTotal(Constance.Null2EmptyTrim(obj[1]));
				
					sumPass=Integer.parseInt(Constance.Null2EmptyTrim(obj[2]))+sumPass;
				asb.setPassNumber(Constance.Null2EmptyTrim(obj[2]));
				
					sumNoPass=Integer.parseInt(Constance.Null2EmptyTrim(obj[3]))+sumNoPass;
				asb.setNoPassNumber(Constance.Null2EmptyTrim(obj[3]));
				
					sumLimit=Integer.parseInt(Constance.Null2EmptyTrim(obj[4]))+sumLimit;
				asb.setLimitNumber(Constance.Null2EmptyTrim(obj[4]));
				
				
					sumUnLimit=Integer.parseInt(Constance.Null2EmptyTrim(obj[5]))+sumUnLimit;
			
				asb.setUnLimitCount(Constance.Null2EmptyTrim(obj[5]));
				
				asbList.add(asb);
			}
			AuditStatisticsBean asb=new AuditStatisticsBean();
			asb.setSumTotal(sumTotal);
			asb.setSumPass(sumPass);
			asb.setSumNoPass(sumNoPass);
			asb.setSumLimit(sumLimit);
			asb.setSumUnLimit(sumUnLimit);
			
			asbList.add(asb);
		}
		return asbList;
	}
	
	
	
	
	public IPageService getPageService() {
		return pageService;
	}

	public void setPageService(IPageService pageService) {
		this.pageService = pageService;
	}

	@Override
	protected BaseSearchCondition getSearchConditionFromSubClass()
			throws Exception {
		return searchCondition;
	}

	@Override
	protected void setSearchConditionToSubClass(
			BaseSearchCondition searchCondition) throws Exception {
		this.setSearchConditionToSubClass(searchCondition);
		
	}

	@Override
	protected void setSearchToConditionSession(
			BaseSearchCondition searchCondition) {
		getSession().setAttribute(SEARCH_SESSION_QUERY_AUDIT_STATISTICS, searchCondition);
		
	}

	@Override
	protected BaseSearchCondition getSearchConditionFromSession() {
		return (AuditStatisticsSearchCondition)getSession().getAttribute(SEARCH_SESSION_QUERY_AUDIT_STATISTICS);
	}

	@Override
	protected String getInitPageSize() {
		return Constance.PAGE_MAX_SIZE+"";
	}

	public AuditStatisticsSearchCondition getSearchCondition() {
		return searchCondition;
	}

	public void setSearchCondition(AuditStatisticsSearchCondition searchCondition) {
		this.searchCondition = searchCondition;
	}

	
}
